﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetX;
using MyMainSite.Repositories;

namespace MyMainSite.Services
{
	/// <summary>
	/// Summary description for DeveloperComments
	/// </summary>
	public class DeveloperComments : IHttpHandler
	{
		public void ProcessRequest(HttpContext context)
		{
			string action = HttpUtility.UrlDecode(context.Request.Params["action"].ToEmptyStringIfNull().ToLower().Trim());

			bool result = false;

			if (action == "addcomment")
			{
				result = this.AddComment(context);
			}

			context.Response.Clear();
			context.Response.Write(result ? "OK" : "ERROR");
		}

		public bool AddComment(HttpContext context)
		{
			string comment = HttpUtility.UrlDecode(context.Request.Params["comment"].ToEmptyStringIfNull().Trim());
			string projectName = HttpUtility.UrlDecode(context.Request.Params["projectname"].ToEmptyStringIfNull().Trim().ToLower());
			string authorName = HttpUtility.UrlDecode(context.Request.Params["name"].ToEmptyStringIfNull().Trim());
			string authorEMailAddress = HttpUtility.UrlDecode(context.Request.Params["email"].ToEmptyStringIfNull().Trim());

			DeveloperCommentRepository repo = new DeveloperCommentRepository();
			return repo.AddComment(comment, projectName, authorName, authorEMailAddress);
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}